#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
void* Routine(void* arg)
{
   pthread_detach(pthread_self());//分离线程
  char*msg =  (char*)arg;
  int count=0;
  while(count<5)
  {
    printf("%s: pid: %d ,tid: %lu\n", msg,getpid() ,getppid(),pthread_self());
    sleep(1);
    count++;
  }
// pthread_exit((void*)19);
   return (void*)10;
}

int main()
{
 pthread_t tid[5];//保存线程的id
 int i=0;
 for(i=0;i<5;i++)
 {
   char* buffer = (char*)malloc(64);
   sprintf(buffer,"thread %d",i);//把要输出的输入进字符串
   pthread_create(&tid[i],NULL,Routine,(void*)buffer);//buffer是给Routine传的参数
   printf("%s tid is: %lu\n",buffer,tid[i]);
 }

 while(1)
 {
 printf("main thread: pid:%d,ppid:%d,tid:%lu\n",getpid(),getppid(),pthread_self());//获取主线程id
sleep(1);
 }
// for(i=0;i<5;i++)
// {
//   void* ret=NULL;
//
//   pthread_join(tid[i],&ret);//等待线程退出
//   printf("thread %d ...quit!!,code:%d\n",i,(int*)ret);
// }
   return  0;
}                   
